package cn.com.pc.content.domain.content;

import cn.com.pc.content.domain.Site;
import cn.com.pc.content.domain.view.ArticleView;
import cn.com.pc.content.domain.view.CustomizedContentView;
import cn.com.pc.content.domain.view.PostView;
import cn.com.pc.content.domain.view.VideoView;
import cn.com.pc.content.dto.ContentDTO;
import cn.com.pc.content.util.BeanUtils;

import java.util.Arrays;
import java.util.List;

/**
 * 内容类型
 */
public enum ContentType {
    Article("article", "文章"),
    Video("video", "视频"),
    AD("ad", "广告"),
    Product("product", "产品"),
    Question("question", "问答"),
    Post("post", "帖子"),
    Audio("audio", "音频"),
    Course("course", "课程"),
    ShortMessage("short_message", "短消息"),
    Other("other", "其它");

    private final String name;
    private final String fullname;

    /**
     * 电脑网内容类型
     */
    public static List<ContentType> pconlineContentTypes = Arrays.asList(Article, Video, Other, ShortMessage);

    /**
     * 亲子网内容类型
     */
    public static List<ContentType> pcbabyContentTypes = Arrays.asList(Article, Video, Other, Question, Audio, Post, Course);

    /**
     * 亲子网内容类型
     */
    public static List<ContentType> pcautoContentTypes = Arrays.asList(Article, Video, Other, Post);

    /**
     * 基本内容类型
     */
    public static List<ContentType> commonContentTypes = Arrays.asList(ContentType.values());

    /**
     * 专用接口类型
     */
    public static List<ContentType> specialContentTypes = Arrays.asList(Article, Video, Post);

    ContentType(String name, String fullname) {
        this.name = name;
        this.fullname = fullname;
    }

    //    @JsonCreator
    public static ContentType getSourceByName(String name) {
        for (ContentType contentType : values()) {
            if (contentType.getName().equals(name)) {
                return contentType;
            }
        }
        return null;
    }

    //    @JsonValue
    public String getName() {
        return name;
    }

    public String getFullname() {
        return this.fullname;
    }

    /**
     * 肉容类型对应实体
     *
     * @return
     */
    public Class<? extends Content> getEntityClass() {
        switch (this) {
            case Article:
                return ArticleView.class;
            case Video:
                return VideoView.class;
            case Post:
                return PostView.class;
            default:
                return CustomizedContentView.class;
        }
    }

    /**
     * 肉容类型对应视图类型
     *
     * @param content
     * @return
     */
    public Content transformEntityClass(Content content) {
        switch (this) {
            case Article:
                ArticleView articleView = new ArticleView();
                BeanUtils.copyProperties(content, articleView);
                return articleView;
            case Video:
                VideoView videoView = new VideoView();
                BeanUtils.copyProperties(content, videoView);
                return videoView;
            case Post:
                PostView postView = new PostView();
                BeanUtils.copyProperties(content, postView);
                return postView;
            default:
                CustomizedContentView customizedContentView = new CustomizedContentView();
                BeanUtils.copyProperties(content, customizedContentView);
                return customizedContentView;
        }
    }

    /**
     * 内容类型对应api前缀
     *
     * @return
     */
    public String getApiPrefix() {
        switch (this) {
            case Article:
                return "/articles";
            case Video:
                return "/videos";
            case Post:
                return "/posts";
            default:
                return "/customized-content";
        }
    }

    /**
     * 内容类型对应DTO类
     *
     * @return
     */
    public Class<? extends ContentDTO> getEntityDTOClass() {
        switch (this) {
            case Article:
                return cn.com.pc.content.dto.ArticleDTO.class;
            case Video:
                return cn.com.pc.content.dto.VideoDTO.class;
            case Post:
                return cn.com.pc.content.dto.PostDTO.class;
            default:
                return cn.com.pc.content.dto.CustomizedContentDTO.class;
        }
    }

    public static List<ContentType> getContentTypes(Site site) {
        switch (site) {
            case PCOnline:
                return pconlineContentTypes;
            case PCBaby:
                return pcbabyContentTypes;
            case PCAuto:
                return pcautoContentTypes;
            default:
                return commonContentTypes;
        }
    }
}
